package com.usvisatrack.core.cloud;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;

/**
 * BcoDepot Amazon Utils class
 * 
 * @author YuCheng Hu
 *
 */
/**
 * Common class to handle the movement of files into the Cloud (in this case Rackspace Cloud Files).
 * 
 * @author YuCheng Hu
 *
 */
public class AwsS3 implements Closeable {

	private static Logger logger = LoggerFactory.getLogger(AwsS3.class);

	private static AwsS3 instance = null;
	private static String accessKey = null;
	private static String secreKey = null;

	/**
	 * Constructor Function
	 */
	public AwsS3() {

		accessKey = "AKIAI6CDTBEK6PF3QBMA";
		secreKey = "F15vscDOpQ3Ac0L08eICSGw55ZlM6qbXgEuboB0B";

	}

	public static AwsS3 getInstance() {
		if (instance == null) {
			instance = new AwsS3();
		}
		return instance;
	}

	/**
	 * Upload files of given directory to Cloud Files Path
	 * 
	 * @param bucketName
	 * @param remotePath
	 * @param file
	 * @throws Exception
	 */
	public static void UploadObjectSingleOperation(String bucketName, String remotePath, File file) throws Exception {

		BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secreKey);
		AmazonS3 s3client = new AmazonS3Client(awsCreds);

		// AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
		try {
			System.out.println("Uploading a new object to S3 from a file\n");

			s3client.putObject(new PutObjectRequest(bucketName, remotePath, file));

		} catch (AmazonServiceException ase) {
			System.out.println("Caught an AmazonServiceException, which " + "means your request made it "
					+ "to Amazon S3, but was rejected with an error response" + " for some reason.");
			System.out.println("Error Message:    " + ase.getMessage());
			System.out.println("HTTP Status Code: " + ase.getStatusCode());
			System.out.println("AWS Error Code:   " + ase.getErrorCode());
			System.out.println("Error Type:       " + ase.getErrorType());
			System.out.println("Request ID:       " + ase.getRequestId());
		} catch (AmazonClientException ace) {
			System.out.println("Caught an AmazonClientException, which " + "means the client encountered "
					+ "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network.");
			System.out.println("Error Message: " + ace.getMessage());
		}

	} // End uploadDirectoryContentsToCloud

	@Override
	public void close() throws IOException {
		// TODO Auto-generated method stub

	}

}
